package com.cyt.chattlibrary.message;

import android.os.Parcel;
import android.text.TextUtils;

import com.cyt.chattlibrary.message.model.MessageContent;
import com.cyt.chattlibrary.message.model.UserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * @author QiQi
 * @ClassName: InformationNotificationMessage
 * @date 2018/10/26
 * @description
 */
public class InformationNotificationMessage extends MessageContent {

    public static final Creator<InformationNotificationMessage> CREATOR = new Creator<InformationNotificationMessage>() {
        @Override
        public InformationNotificationMessage createFromParcel(Parcel source) {
            return new InformationNotificationMessage(source);
        }

        @Override
        public InformationNotificationMessage[] newArray(int size) {
            return new InformationNotificationMessage[size];
        }
    };
    private String message;

    public InformationNotificationMessage(byte[] data) {
        String jsonStr = null;

        try {
            jsonStr = new String(data, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            if (jsonObject.has("message")) {
                setMessage(jsonObject.optString("message"));
            }

            if (jsonObject.has("user")) {
                setUserInfo(parseJsonToUserInfo(jsonObject.getJSONObject("user")));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    protected InformationNotificationMessage(Parcel in) {
        this.message = in.readString();
        setUserInfo(in.readParcelable(UserInfo.class.getClassLoader()));
    }

    private InformationNotificationMessage() {

    }

    public static InformationNotificationMessage obtain(String message) {
        InformationNotificationMessage model = new InformationNotificationMessage();
        model.setMessage(message);
        return model;
    }

    public String getMessage() {
        return message == null ? "" : message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObject = new JSONObject();

        try {
            if (!TextUtils.isEmpty(message)) {
                jsonObject.put("message", message);
            }

            if (getJSONUserInfo() != null) {
                jsonObject.put("user", getJSONUserInfo());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            return jsonObject.toString().getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.message);
        dest.writeParcelable(getUserInfo(), flags);
    }
}
